const upload = require('./upload');
const router = require('express').Router();
const photoController = require('../../controllers/uploadController');
const Jwt = require('../../common/jwt');
// 上传和预览图片
router.post('/preview', upload.single('photo'),  async (req,res)=>{
    let token = req.body.token || req.headers.token;
    let jwt = new Jwt(token);
    // 判断token是否在有效期
    let result = jwt.verifyToken();
    if(result.data){
        await photoController.preview(req,res)
    }
    else {
        res.send({
            code: -1,
            msg: '登录已过期,请重新登录'
        })
    }
})

// 删除图片
router.post('/delete', async (req,res)=>{
    await photoController.delete(req,res);
})

// 上传图片（符合富文本框的上传图片的接口）
router.post('/upload', upload.single('photo'),  async (req,res)=>{
  
    let token = req.body.token || req.headers.token || req.query.token;
    // console.log('1.0',req.body.token);// 有值
    // console.log('2.0',req.headers.token)
    // console.log('3.0',req.query.token)
    let jwt = new Jwt(token);
    // 判断token是否在有效期
    let result = jwt.verifyToken();
    if(result.data){
        // 调用富文本组件上传图片的接口
        await photoController.editorUpload(req,res)
    }
    else {
        res.send({
            errno: -1,
            code: -1,
            msg: '登录已过期,请重新登录'
        })
    }
})

module.exports = router;